package com.seaboxdata.sm4;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

//import sun.misc.BASE64Decoder;
//import sun.misc.BASE64Encoder;

/*
 * AES对称加密和解密
 */
public class AESEncoder {

    private static final String characterEncoding       = "UTF-8";
    private static final String cipherTransformation    = "AES/ECB/PKCS5PADDING";
    private static final String aesEncryptionAlgorithem = "AES";

    private static final String key = "Pfdzws_@20220306";
    /**
     * Method for Encrypt Plain String Data
     * @param plainText
     * @return encryptedText
     */
        public static String AESEncode(String encrptyKey,String plainText) {

        String encryptedText = "";
        try {
        	if (encrptyKey.length()>16)
        		encrptyKey=encrptyKey.substring(0,16);
            Cipher cipher   = Cipher.getInstance(cipherTransformation);
            byte[] key      = encrptyKey.getBytes(characterEncoding);
            SecretKeySpec secretKey = new SecretKeySpec(key, aesEncryptionAlgorithem);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] cipherText = cipher.doFinal(plainText.getBytes(characterEncoding));
            Base64.Encoder encoder = Base64.getEncoder();
            encryptedText = encoder.encodeToString(cipherText);

        } catch (Exception E) {
             System.err.println("Encrypt Exception : "+E.getMessage());
        }
        return encryptedText;
    }

    /**
     * Method For Get encryptedText and Decrypted provided String
     * @param encryptedText
     * @return decryptedText
     */
    public static String AESDncode(String encrptyKey,String encryptedText) {
        String decryptedText = "";
        try {
        	if (encrptyKey.length()>16)
        		encrptyKey=encrptyKey.substring(0,16);
            Cipher cipher = Cipher.getInstance(cipherTransformation);
            byte[] key = encrptyKey.getBytes(characterEncoding);
            SecretKeySpec secretKey = new SecretKeySpec(key, aesEncryptionAlgorithem);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] cipherText = decoder.decode(encryptedText.getBytes(characterEncoding));
            decryptedText = new String(cipher.doFinal(cipherText), characterEncoding);

        } catch (Exception E) {
            System.err.println("decrypt Exception : "+E.getMessage());
        }
        return decryptedText;
    }

    public static void main(String[] args) {
        String test = AESEncoder.AESEncode(key, "测试字段");
        System.out.println(test);
        System.out.println(AESEncoder.AESDncode(key, "TdYawpvcZkBY49/dMuiI0Q=="));
        String s="{\"label\":\"yss\",\"cid\":\"513************014\",\"cidtype\":\"1000\",\"name\":\"侯*江\",\"phone\":\"183******71\",\"encode\":\"EgoxIzXSDETahTFpsgX29e3o0TXz/ZiLjd+Cvu3YMFbC+0ES86yzEFY9Fpry5p8H\",\"c\":\"G\",\"t\":1646639735,\"v\":2,\"s\":\"fbEqQc3RFsAkpvnk4cD0GIWI3vqKOJbrx20ZP50hf1BHa3z0DB0eU/43weaG00JcLR0AtzxuU6MtbGYhiLxoPg==\"}";
        System.out.println(s.length());

        String test1 = AESEncoder.AESDncode(key, "ExGU9ULWm63x4Cgawv6qbQ==");
        System.out.println(test1);
    }

}
